layui.use(['form','jquery','layer','laydate'], function(){
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;
    var laydate = layui.laydate;

    laydate.render({
        elem:"#startDate",
        type:"datetime",
        trigger:"click"
    });
    laydate.render({
        elem:"#endDate",
        type:"datetime",
        trigger:"click"
    });
    form.verify({
        couponName: function(value, item){ //value：表单的值、item：表单的DOM对象
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '优惠券名称不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '优惠券名称首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '优惠券名称不能全为数字';
            }
        }
    });
    //审核
    addCoupons = function addCoupons() {
        var createId = localStorage.getItem("userId");
        var createDate = new Date();
        createDate = getTime(createDate);
        var json={
            couponName:$("#couponName").val(),
            couponPrice:$("#couponPrice").val(),
            startDate:$("#startDate").val(),
            endDate:$("#endDate").val(),
            online:$("input[name=online]").val(),
            grant:$("#grant").val(),
            issueQuantity:$("#issueQuantity").val(),
            type:$("#type").val(),
            createId:createId,
            createDate:createDate
        };
        if(json.couponName==""|json.couponPrice==""|json.startDate==""|json.endDate==""|json.online==""|json.grant=="1"|json.issueQuantity==""|json.type=="1"){
            layer.msg("还没有完善好信息哦~");
        }else{
            $.ajax({
                type:"post",
                dataType:"json",
                data:json,
                url:"/Coupons/addCoupons",
                success:function (data) {
                    if(data){
                        layer.msg("添加成功");
                    }else{
                        layer.msg("添加失败");
                    }
                }
            });
        }
    };
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    $("#shuaxin").click(function () {
        location.reload();
    });
    //跳转
    goBack=function goBack(){
            //通过页面查找iframe
            parent.$('#recordLog').val($('#examine').text());
            parent.before();
            return false;
        };
});
